/*
 * $Id$
 *
 * Copyright 2006- Revolution Health Group.  All rights reserved.
 *
 * This software is the confidential and proprietary information
 * of Revolution Health Group.  (Confidential Information).
 * You shall not disclose such Confidential Information and shall
 * use it only in accordance with the terms of the license 
 * agreement you entered into with Revolution Health Group.
 *
 */
package org.javason.jsonrpc;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import net.sf.json.JSONArray;
import net.sf.json.JSONException;
import net.sf.json.JSONObject;
import net.sf.json.util.JSONUtils;

import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.web.servlet.ModelAndView;

import org.javason.json.BeanToJsonSerializer;
import org.javason.util.ClassUtils;

import junit.framework.TestCase;

public class TestJsonRpcJaxBArray extends TestCase {
	
	public void testJsonInterface() {
		System.out.println("RUNNING TestJsonRpcJaxBArray.testJsonInterface()");
		String requestURI = "/world";
		MockHttpServletRequest request = new MockHttpServletRequest("POST", requestURI);
		MockHttpServletResponse response = new MockHttpServletResponse();
		request.setPathInfo(requestURI);
		
		JSONObject doSomething = new JSONObject();
		JSONObject testId = new JSONObject();
		try {
			testId.put("someCtx", false);
			doSomething.put("id", testId);
		
			doSomething.put("method", "testJaxBArray");
			JSONArray param1 = new JSONArray();
			JsonBean3 tjb = new JsonBean3();
			tjb.getTestArray().add("String Here");
			
			JSONObject paramObj = BeanToJsonSerializer.serializeToJsonObject(tjb);
			System.out.println(paramObj.toString());
			
			assertEquals(1, paramObj.size());
			assertEquals(1, paramObj.getJSONArray("testArray").size());
			assertEquals("String Here", paramObj.getJSONArray("testArray").getString(0));

			param1.add(0, paramObj);
			doSomething.put("params", param1);
		
			request.setContent(doSomething.toString().getBytes("UTF-8"));
		} catch (JSONException e2) {
			// TODO Auto-generated catch block
			e2.printStackTrace();
			assertTrue(false);
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			assertTrue(false);
		}
		
		try {
			JsonRpcServiceExporter exporter = new JsonRpcServiceExporter();
			exporter.setServiceBean(new JsonInterface());
			
			ModelAndView mv = exporter.handleRequest(request, response);
			
			JsonRpcServiceView iv = new JsonRpcServiceView();
			iv.render(mv.getModel(), request, response);
			System.out.println(response.getContentAsString());
			
			JSONObject responseObj = JSONObject.fromObject(response.getContentAsString());
			
			assertTrue(responseObj.has("result"));
			assertTrue(JSONUtils.isNull(responseObj.opt("error")));
			assertTrue(responseObj.getJSONObject("id").toString().equals(testId.toString()));
			

			JSONObject paramObj = responseObj.getJSONObject("result");

			assertEquals(1, paramObj.size());
			assertEquals(1, paramObj.getJSONArray("testArray").size());
			assertEquals("String Here", paramObj.getJSONArray("testArray").getString(0));
			
		} catch (Exception e1) {
			e1.printStackTrace();
			assertTrue(false);
		}
		System.out.println("--");
	}

	public void testXmlEnum() throws JSONException {
		Object result = ClassUtils.findEnumConstant(PlanInformationTypeList.class, "SummaryBenefits");
		this.assertNotNull(result);
		Object result1 = BeanToJsonSerializer.serialize(PlanInformationTypeList.SUMMARY_BENEFITS);
		System.out.println("result 1  = " + result1.toString());
		this.assertEquals("SummaryBenefits",result1.toString());
		
		TestXmlEnumHolder tmp = new TestXmlEnumHolder();
		tmp.setPlan(PlanInformationTypeList.SUMMARY_BENEFITS);
		Object result2 = BeanToJsonSerializer.serialize(tmp);
		System.out.println("result 2  = " + result2.toString());
		this.assertEquals("SummaryBenefits", ((JSONObject)result2).getString("plan") );
	}
	
	
	public void testEnum() {
		Object result = ClassUtils.findEnumConstant(EnumTest.class, EnumTest.Spouse);
		this.assertNotNull(result);
		result = ClassUtils.findEnumConstant(EnumTest.class, EnumTest.Son.toString());
		this.assertNotNull(result);
		
		Object result1 = BeanToJsonSerializer.serialize(EnumTest.Son);
		this.assertEquals(result1.toString(), EnumTest.Son.name());
		this.assertEquals(result1.toString(), EnumTest.Son.toString());
		this.assertEquals(result1.toString(), "Son");
	}
	
	
	public class TestXmlEnumHolder {
		private PlanInformationTypeList _plan;
		public PlanInformationTypeList getPlan() { return _plan; }
		public void setPlan(PlanInformationTypeList plan) { _plan = plan; }
	}
	
	public enum EnumTest {
		Spouse,
		Son,
		Daughter;
	}
}
